$(function(){
	//加载所有菜单
	$('#menu').fileTree({
		root: 'source',
		script: 'sysMenuAction!queryMenuAll.ac',
		expandEasing: 'easeOutBounce',
		collapseEasing: 'easeOutBounce',
		expandAll: true,
		showLeaf: false,
		showCheckbox: true,
		onlyOneCanChecked: true,
		complete: function(){
			var privilegeId = $('#sysPrivilegeMenuId').val();
			$('#menu :checkbox').each(function(){
				if($(this).val() == privilegeId){
					$(this).click();
				}
			});
		}
	});
	
	//提交事件
	$('#submitBtn').click(function(){
		var $i = $('#sysPrivilegeIdHidden');
		var $n = $('#sysPrivilegeName');
		var $u = $('#sysPrivilegeUrl');
		var $c = $('#menu :checkbox[checked]');
		
		if($n.val() == ''){
			jAlert('名称必填');
			return;
		};
		
		if($u.val() == ''){
			jAlert('路径必填');
			return;
		};
		
		if($c.size() != 1){
			jAlert('必须且只能选择一个所属栏目');
			return;
		}
		
		function collectData(){
			var a = new Array();
			a.push('sysPrivilege.id=' + $i.val());
			a.push('sysPrivilege.name=' + $n.val());
			a.push('sysPrivilege.url=' + $u.val());
			a.push('sysPrivilege.menuId=' + $c.val());
			return a.join('&');
		}

		
		$.ajax({
			type:'POST',
			url: 'sysPrivilegeAction!updatePrivilege.ac',
			data: collectData(),
			dataType:'json',
			beforeSend:function(){
				jLoading("正在操作，请稍后...");
			},
			success:function(rs){
				if(rs.flag){
					jAlert('操作成功');
				}else{
					jAlert('操作失败:' + rs.msg);
				}
			}
		});
	});
});